package com.zhao.modleclass.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zhao.modleclass.R;
import com.zhao.modleclass.adapter.BiaoQianAdapter;
import com.zhao.modleclass.adapter.SC_MoDenTeacherAdapter;
import com.zhao.modleclass.adapter.SC_TeacherRActivityAdapter;
import com.zhao.modleclass.bean.SearchClassBiaoQianBean;
import com.zhao.modleclass.presenter.SearchPresenter;
import com.zhao.modleclass.utils.SpacesItemDecoration;
import com.zhao.modleclass.view.ISearchCLassView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 张昭 on 2016/12/9.
 */

public class SCTeacherListFragment extends Fragment implements ISearchCLassView{
    private Context mContext;
    private RecyclerView recyclerView;
    private SearchPresenter searchPresenter;
    private int tag;
    private int tage=0;
    private PullToRefreshScrollView refreshLayout;
    private int page=2;;
    public SCTeacherListFragment(int tag) {
        this.tag = tag;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.item_mdls_sct,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView= (RecyclerView) view.findViewById(R.id.recyclerView_fragment_sctl);
        refreshLayout= (PullToRefreshScrollView) view.findViewById(R.id.refresh_item_mdls_sct);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final LinearLayoutManager manager=new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new SpacesItemDecoration(3));
        searchPresenter=new SearchPresenter(this);
        //teacherList.do?techLevelId=0&pageSize=10&pageNo=1

        final Map<String, String> map = new HashMap<>();
        switch (tag){
            case 0:
                map.put("techLevelId", "0");
                break;
            case 1:
                map.put("techLevelId", "21");
                break;
            case 2:
                map.put("techLevelId", "11");
                break;
        }
        map.put("pageSize", "10");
        map.put("pageNo", "1");
        searchPresenter.loginTeacher(mContext, "teacherList.do", map,recyclerView,tage);
        refreshLayout.setMode(PullToRefreshBase.Mode.BOTH);
        refreshLayout.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                tag=0;
                map.put("pageNo", "1");
                searchPresenter.loginTeacher(mContext, "teacherList.do", map,recyclerView,tage);
                refreshLayout.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                tage=1;
                map.put("pageNo", page+"");
                searchPresenter.loginTeacher(mContext, "teacherList.do", map,recyclerView,tage);
                page++;
                refreshLayout.onRefreshComplete();
            }
        });

    }



    @Override
    public void toSetAdapter(BiaoQianAdapter adapter) {

    }

    @Override
    public void showError(Exception e) {

    }

    @Override
    public void getTeacher(SearchClassBiaoQianBean searchClassBiaoQianBean) {

    }

    @Override
    public void getTatle(List<SearchClassBiaoQianBean.TagListBean> mList) {

    }

    @Override
    public void toSetItemAdapter(SC_MoDenTeacherAdapter tAdapter) {
        if (tAdapter != null) {
            recyclerView.setAdapter(tAdapter);
            refreshLayout.onRefreshComplete();
        }
    }

    @Override
    public void toSetRAdapter(SC_TeacherRActivityAdapter adapter) {

    }
}
